import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class DataManager {
    private static final String DATA_FILE = "school_data.dat";
    
    // 保存所有数据
    public static void saveData(List<Class> classes, List<Group> groups, List<Student> students) {
        try (ObjectOutputStream oos = new ObjectOutputStream(
                new FileOutputStream(DATA_FILE))) {
            oos.writeObject(classes);
            oos.writeObject(groups);
            oos.writeObject(students);
        } catch (IOException e) {
            e.printStackTrace();
            System.err.println("保存数据失败：" + e.getMessage());
        }
    }
    
    // 加载所有数据
    @SuppressWarnings("unchecked")
    public static void loadData(List<Class> classes, List<Group> groups, List<Student> students) {
        File file = new File(DATA_FILE);
        if (!file.exists()) {
            return; // 如果文件不存在，直接返回
        }
        
        try (ObjectInputStream ois = new ObjectInputStream(
                new FileInputStream(file))) {
            List<Class> loadedClasses = (List<Class>) ois.readObject();
            List<Group> loadedGroups = (List<Group>) ois.readObject();
            List<Student> loadedStudents = (List<Student>) ois.readObject();
            
            // 清除现有数据并添加加载的数据
            classes.clear();
            classes.addAll(loadedClasses);
            
            groups.clear();
            groups.addAll(loadedGroups);
            
            students.clear();
            students.addAll(loadedStudents);
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
            System.err.println("加载数据失败：" + e.getMessage());
        }
    }
} 